<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Film extends LW_Controller {

    public function __construct()
    {
        parent::__construct();
        // 初始化样式
        $this->load->library('lw_db',array('tb_name'=>'tb_film_list'),'tb_film_list');
        $this->load->library('lw_db',array('tb_name'=>'tb_film_category'),'tb_film_category');
        $this->assets = $this->lw_assets->getAssets();
    }

    public function index($cateName='dongzuo')
    {
        $this->assets['pageCss'] = array(base_url('assets/pages/film/css/index.css'));
        $data = $this->data; // 继承的数据
        $data['assets'] = $this->assets;
        $cateInfo = $this->tb_film_category->get_one(array('name_pinyin'=>$cateName));
        $data['pageTitle'] =  ($cateInfo['name_cn'])?$cateInfo['name_cn']:'最新电影';
        $cateId = ($cateInfo['id'])?$cateInfo['id']:0;
        $page = ($this->input->get('page'))?$this->input->get('page'):1;
        $data['listHtml'] =  $this->lists($cateId,$cateName,$page);
        $this->lw_load->view('film/index',$data);
    }

    public function lists($cateId,$cateName,$page)
    {
        $this->load->library('lw_pagination');
        $sql = "SELECT
                    *
                FROM
                    tb_film_list WHERE has_download=1";
        if($cateId!=0){
            $sql .= " AND cate_id = {$cateId}";
        }
        $hasWhere = true;
        $group = null;
        $order = "id desc";
        $paramFilter = null;
        $action = "/cate/{$cateName}";
        $pageSize = 24;
        $type = 2;
        $pageInfo = $pagination = $this->lw_pagination->normalPagination($sql,null,$page,$hasWhere,$group,$order,$paramFilter,$action,$pageSize,$type);
        $pageData['dataList'] = $pageInfo['pageData'];
        $pageData['pagination'] = $pageInfo['pageConfig'];
        $pageData['pageHtml'] = $this->load->view('system/pagination/pagination',$pageData,true);
        return $this->load->view('film/list',$pageData,true);
    }

    /**
     * @param $id 电影ID
     */
    public function detail($id)
    {
        $this->load->library('lw_thunder');
        $data = $this->data;
        $this->load->library('lw_db',array('tb_name'=>'tb_film_download'),'tb_film_download');
        $this->assets = $this->lw_assets->getAssets(['clipboard']);
        $this->assets['pageCss'] = array(base_url('assets/pages/film/css/detail.css'));
        $data['assets'] = $this->assets;
        $data['info'] = $this->tb_film_list->get_one(array('id'=>$id));
        $cateInfo = $this->tb_film_category->get_one(array('id'=>$data['info']['cate_id']));
        $data['cateName'] = $cateInfo['name_cn'];
        $downloadList = $this->tb_film_download->get_all('*',array('film_id'=>$id));
        for($i=0;$i<sizeof($downloadList);$i++){
            $downloadList[$i]['download_code'] = $this->lw_thunder->thunderEncode($downloadList[$i]['download_url']);
        }
        $data['downloadList'] = $downloadList;
        $this->lw_load->view('film/detail',$data);
    }
}
